package com.cxhome.cxinfox.cxmenu;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.system.Module;
import com.traffic.managent.util.SQLUtils;

/**
 * 数据字典service
 * 
 */
public class ModuleService extends BaseService {

	public static final ModuleService service = new ModuleService();
	
	public Page<Module> paginate(Module model, Paginator paginate) {

		SQLUtils sql = new SQLUtils(" from cx_module t "
				+ " left join cx_module p on t.parentmoduleids = p.ids  left join cx_systems s on t.systemsids = s.ids where 1=1 ");
		if (model.getStr("names") != null && !"".equals(model.getStr("names"))) {
			// 查询条件
			sql.whereLike("t.names", model.getStr("names").trim());
		}

		return Module.dao.paginate(paginate, "select t.*,p.names as parentName,s.names as sysnames", sql.toString());

	}
	
	
	public void saveEdit(Module model) {
		Module parentmodel = Module.dao.findFirst("select * from cx_department where 1=1 and ids = ?",
				model.getStr("parentdepartmentids"));
		String pathName = parentmodel.getStr("pathsName");
		String paths = parentmodel.getStr("paths");

		pathName += "/" + model.getStr("names");
		paths += "/" + model.getStr("ids");

		model.put("pathsName", pathName);
		model.put("paths", paths);

		model.update();
	}
	
	/**
	 * 获取模块下拉框
	 * 
	 * @return
	 */
	public String selectModule(String selected) {

		List<Module> list = Module.dao.find(" select * from cx_module ");

		StringBuffer sb = new StringBuffer("");
		if (list != null && list.size() > 0) {
			for (Module department : list) {
				String parentId = department.getStr("parentmoduleids");
				if ("".equals(parentId) || parentId == null) {
					String id = department.getStr("ids");
					sb.append("<option value=\"");
					sb.append(id);
					sb.append("\" ");
					boolean flag = (selected != null && id.equals(selected));
					sb.append(flag ? "selected" : "");
					sb.append(">");
					sb.append(department.getStr("names"));
					sb.append("</option>");

					sb.append(recursionTree(id, selected, 1, list));
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 递归树
	 * @return
	 */
	public String recursionTree(String rootId, String selected, int level, List<Module> list) {
		if (list == null || list.size() <= 0) {
			return "";
		}
		// 级别太多了
		if (level >= 20) {
			return "";
		}

		StringBuffer sb = new StringBuffer("");
		for (Module department : list) {
			String parentId = department.getStr("parentmoduleids");

			if (rootId.equals(parentId)) {
				String id = department.getStr("ids");
				sb.append("<option value=\"");
				sb.append(id);
				sb.append("\" ");
				boolean flag = (selected != null && id.equals(selected));
				sb.append(flag ? "selected" : "");
				sb.append(">");
				for (int i = 0; i < level; i++) {
					if (i == (level - 1)) {
						sb.append("&nbsp;|--");
					} else {
						sb.append("&nbsp;&nbsp;&nbsp;");
					}
				}
				sb.append(department.getStr("names"));
				sb.append("</option>");

				sb.append(recursionTree(id, selected, (level + 1), list));
			}
		}

		return sb.toString();
	}

	/**
	 * 获取名称
	 * @param model
	 * @return
	 */
	public String getmoduleName(Integer id) {
		if (id == null || id == 0) {
			return "";
		}
		String name = Module.dao.findById(id).getStr("name");
		return name;
	}

}
